package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.util.aDebug;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class InfoListFrg extends ListFragment
{
	public  static final int NUM_ITEMS = 7;
    
    private int m_nCurSelectedPos;
    private String[] ITEMS_TITLE;//, ITEMS_DESC;
    
    private static final String EXTRA_PREFIX = InfoListFrg.class.getName();
    private static final String EXTRA_CUR_SELECTED_POS = EXTRA_PREFIX + ".csp";
    public  static final String EXTRA_SEL_ITEM_POS = EXTRA_PREFIX + ".sip";
    public  static final String EXTRA_SEL_ITEM_CHANGED = EXTRA_PREFIX + ".sic";
    public  static final String EXTRA_SEL_CHANGED_MANUALLY = EXTRA_PREFIX + ".scm";
    
    private static final String LOG_TAG = InfoListFrg.class.getSimpleName();
    
    ////////////////////////////////////////
    @Override public void 
    onCreate(Bundle bdSavedInstanceState)
    {
        super.onCreate(bdSavedInstanceState);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onCreate()");
        
        ITEMS_TITLE = getResources().getStringArray(R.array.info_indicator);
        //ITEMS_DESC = getResources().getStringArray(R.array.stat_items_desc);
        
        m_nCurSelectedPos = -1;
        
        _ReloadFromArguments(getArguments());
    }
    
    @Override public void 
    onActivityCreated(Bundle bdSavedInstanceState)
    {
        super.onActivityCreated(bdSavedInstanceState);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "onActivityCreated()");
        
        int nLastSelectedPos = m_nCurSelectedPos;
        if (bdSavedInstanceState != null)
            nLastSelectedPos = bdSavedInstanceState.getInt(EXTRA_CUR_SELECTED_POS, m_nCurSelectedPos);
        
        //Context ctx = getActivity();
        
        ListView lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lv.setItemChecked(m_nCurSelectedPos, true);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "  m_nCurSelectedPos=" + m_nCurSelectedPos);
        
        _SelectionChanged(nLastSelectedPos, false);
    }
    
    @Override public void 
    onSaveInstanceState(Bundle bdOutState)
    {
        super.onSaveInstanceState(bdOutState);
        bdOutState.putInt(EXTRA_CUR_SELECTED_POS, m_nCurSelectedPos);
    }

    public void
    SetItemChecked(int nPos)
    {
        ListView lv = getListView();
        lv.setItemChecked(nPos, true);
    }
    
    private void 
    _ReloadFromArguments(Bundle bdArguments) 
    {
        setListAdapter(new MyListAdapter());
    }
    
    @Override public void 
    onListItemClick(ListView lv, View vItem, int nPos, long lID)
    {
        _SelectionChanged(nPos, true);
    }

    private void
    _SelectionChanged(int nNewCheckedPos, boolean bChangedManually)
    {
        int nOldCheckedPos = m_nCurSelectedPos;
        m_nCurSelectedPos = nNewCheckedPos;

        BaseActivity activity = (BaseActivity)getActivity();
        if (activity != null) {
            Intent it = new Intent();
            it.putExtra(EXTRA_SEL_ITEM_POS, nNewCheckedPos);
            it.putExtra(EXTRA_SEL_ITEM_CHANGED, nNewCheckedPos != nOldCheckedPos);
            it.putExtra(EXTRA_SEL_CHANGED_MANUALLY, bChangedManually);

            activity.notifyByFragment(this, it);
        }
    }
    
    //////////////////////////////
    private class MyListAdapter extends BaseAdapter {
        private LayoutInflater m_lf;
        
        MyListAdapter()
        {
            m_lf = LayoutInflater.from(getActivity());
        }
        
        @Override public int 
        getCount()
        {
            return ITEMS_TITLE.length;
        }

        @Override public String 
        getItem(int nPos)
        {
            return ITEMS_TITLE[nPos];
        }

        @Override public long 
        getItemId(int nPos)
        {
            return nPos;
        }

        @Override public View 
        getView(int nPos, View vConvert, ViewGroup vgParent)
        {
            if (vConvert == null) {
                vConvert = m_lf.inflate(R.layout.info_list_title, vgParent, false);
            }
            
            TextView tvTitle = (TextView)vConvert.findViewById(R.id.title);
            //TextView tvInfo = (TextView)vConvert.findViewById(R.id.info);
            
            tvTitle.setText(ITEMS_TITLE[nPos]);
            //tvInfo.setText(ITEMS_DESC[nPos]);
            return vConvert;
        }
    }
}
